using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;


namespace OA.UI
{
    [UI.SecurityCheck]
    public abstract class BaseController:Controller
    {
        public BaseController()
        {
        }

        #region 性能统计

        private System.Diagnostics.Stopwatch time = null;

        /// <summary>
        /// 是否统计性能
        /// </summary>
        protected virtual bool IsPerformance { get { return true; } }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (IsPerformance && BLL.SystemConfig.LoadConfig().Feature.IsPerformance)
            {
                time = new System.Diagnostics.Stopwatch();
                time.Start();
            }
            base.OnActionExecuting(filterContext);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (IsPerformance && time != null && BLL.SystemConfig.LoadConfig().Feature.IsPerformance)
            {
                time.Stop();
                var pathAndQuery = Request.Url.PathAndQuery;
                if (Request.Form.Count > 0)
                {
                    pathAndQuery += ":" + Request.Form.ToString();
                }
                Model.Performance per = new Model.Performance()
                {
                    ActionName = filterContext.ActionDescriptor.ActionName,
                    ControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                    ElapsedMilliseconds = time.ElapsedMilliseconds,
                    PathAndQuery = pathAndQuery,
                    Time = DateTime.Now
                };
                new BLL.Performance().Add(per);
            }
            base.OnActionExecuted(filterContext);
        }
        #endregion
    }
}
